var $playerModal = $("#playerModal");
var $playerForm = $("#playerForm");

$playerModal.delegate('.js-submit-player', 'click', function() {
    $playerForm.data('bootstrapValidator').validate();
    if (!$playerForm.data('bootstrapValidator').isValid()) {
        return false;
    }
    if (!confirm("确定要提交保存？")) {
        return false;
    }

    var url = BASE_PATH + '/front/player/save';
    var param = serializeArrayToJson($playerForm.serializeArray());
    $.ajax({
        url: url,
        type: 'post',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: JSON.stringify(param),
        success: function(resp) {
            if (resp.code != 0) {
                doAlert(resp.message || '操作失败！');
                return false;
            }
            window.localStorage.setItem("mobile", param.mobile);
            doAlert('报名成功！请到手机官网中“我的-我的门票”页面查看入场券并截图保存', 1500, 'success', function() {
                $playerModal.modal('hide');
                if (toSuccessPage) {
                    window.location.href = BASE_PATH + "/front/register/success.html";
                }
                if ($('.container').hasClass('register')) {
                    window.location.href = BASE_PATH + "/front/my.html";
                }
            })
        }
    })
})

function initPlayerValidator() {
    $playerForm.bootstrapValidator({
        container: 'tooltip',
        excluded: [":disabled"],
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        }
    });
}

$(document).ready(function() {
    initPlayerValidator();
})